//
//  ILHomeArticTableViewController.m
//  ILife
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILHomeArticTableViewController.h"
#import "ILHomeArticTableViewCell.h"
#import "FMDatabase.h"
#import "ILFashionDetailBasonModel.h"
#import "ILFashionWebViewController.h"
#import "ILHomePageModel.h"


@interface ILHomeArticTableViewController ()

@property (nonatomic, strong) FMDatabase *dataBase;
@property (nonatomic, strong) UIImageView *thumbImg;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;





@end

@implementation ILHomeArticTableViewController

- (NSMutableArray *)dataSourceArray
{
    if (_dataSourceArray == nil) {
        self.dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
    
    // 创建数据库/打开数据库
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(9, 1, 1).firstObject stringByAppendingPathComponent:@"ILArticCollcet.db"];
    
    
    _dataBase = [FMDatabase databaseWithPath:filePath];
    
    if ([_dataBase open]) {
        NSLog(@"数据库打开成功");
    }
    else
    {
        NSLog(@"数据库打开失败");
    }

    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from artic"];
    if ([resultSet next] == NO) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有收藏呢 快去收藏一下吧！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem;
     self.navigationItem.title = @"文章收藏";
    [self.tableView registerClass:[ILHomeArticTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    // 创建数据库/打开数据库
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(9, 1, 1).firstObject stringByAppendingPathComponent:@"ILArticCollcet.db"];
    
    _dataBase = [FMDatabase databaseWithPath:filePath];
    
    if ([_dataBase open]) {
        NSLog(@"数据库打开成功");
    }
    else
    {
        NSLog(@"数据库打开失败");
    }
    
    FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"select * from artic"];
    
    while ([resultSet next]) {
        NSString *contentid = [resultSet stringForColumn:@"contentid"];
        NSString *thumb = [resultSet stringForColumn:@"thumb"];
        NSString *title = [resultSet stringForColumn:@"title"];
        NSString *home_contentid = [resultSet stringForColumn:@"home_contentid"];
        
        ILHomePageModel *homeModel = [[ILHomePageModel alloc]init];
        
        homeModel.contentid = contentid;
        homeModel.thumb = thumb;
        homeModel.title = title;
        homeModel.home_contentid = home_contentid;
        
        
        [self.dataSourceArray addObject:homeModel];
    }
    [resultSet close];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ILHomeArticTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   
    cell.configuer = _dataSourceArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ILFashionWebViewController *webVC = [[ILFashionWebViewController alloc]init];
    
    webVC.contentid = ((ILHomePageModel *) _dataSourceArray[indexPath.row]).contentid;
    webVC.home_contentid = ((ILHomePageModel *) _dataSourceArray[indexPath.row]).home_contentid;
    webVC.from = @"1";
    
    [self.navigationController pushViewController:webVC animated:YES];
    
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [_dataBase executeUpdate:@"delete from artic where contentid = ?",((ILHomePageModel *) _dataSourceArray[indexPath.row]).contentid];
        
        [self.dataSourceArray removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
